import { useArticleStore } from "@/store/modules/article";
import { rangeHandler } from "./range";
const articleStore = useArticleStore();

export function updateTextContent(target: HTMLElement) {
  const section = getSelection()!;
  const anchorOffset = section.anchorOffset;
  const position = rangeHandler.getCursorPosition(true);

  const articleId = articleStore.getCurrentArticle!.id;
  articleStore.updateArticleHtmlContent(articleId, target.innerHTML);

  nextTick(() => {
    const node = rangeHandler.findNodeByPosition(position);
    rangeHandler.createAndInsertRange(node, anchorOffset);
  });
}
